﻿using System;
using System.ComponentModel;
using Aeon.Configuration;

namespace Aeon.Emulator.Sound.Blaster
{
    /// <summary>
    /// Describes Sound Blaster DSP settings.
    /// </summary>
    [Category("Sound")]
    [DeviceConfiguration("Aeon.SoundBlaster")]
    public sealed class SoundBlasterConfiguration : DeviceConfiguration
    {
        private int irq = 7;
        private int dma8 = 1;
        private int dma16 = 5;

        /// <summary>
        /// Initializes a new instance of the SoundBlasterConfiguration class.
        /// </summary>
        public SoundBlasterConfiguration()
            : base("Sound Blaster DSP")
        {
        }

        /// <summary>
        /// Gets or sets the Sound Blaster IRQ.
        /// </summary>
        [Browsable(true)]
        [DefaultValue(7)]
        [RangeConstraint(0, 15)]
        public int IRQ
        {
            get
            {
                return this.irq;
            }
            set
            {
                if(value != this.irq)
                {
                    this.irq = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("IRQ"));
                }
            }
        }
        /// <summary>
        /// Gets or sets the 8-bit DMA channel.
        /// </summary>
        [Browsable(true)]
        [DefaultValue(1)]
        [DisplayName("8-bit DMA")]
        [RangeConstraint(0, 3)]
        public int DMA8
        {
            get
            {
                return this.dma8;
            }
            set
            {
                if(value != this.dma8)
                {
                    this.dma8 = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DMA8"));
                }
            }
        }
        /// <summary>
        /// Gets or sets the 16-bit DMA channel.
        /// </summary>
        [Browsable(true)]
        [DefaultValue(5)]
        [DisplayName("16-bit DMA")]
        [RangeConstraint(5, 7)]
        public int DMA16
        {
            get
            {
                return this.dma16;
            }
            set
            {
                if(value != dma16)
                {
                    this.dma16 = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DMA16"));
                }
            }
        }

        /// <summary>
        /// Returns a new instance of the emulated device.
        /// </summary>
        /// <param name="p">Extra parameters which may be used for initialization.</param>
        /// <returns>New instance of the emulated device.</returns>
        public override IVirtualDevice Create(CreateDeviceParameters p)
        {
            if(p == null)
                throw new ArgumentNullException("p");

            return new SoundBlaster(p.VirtualMachine, p.WindowHandle, this.IRQ, this.DMA8, this.DMA16);
        }
        /// <summary>
        /// Returns a deep copy of the device configuration.
        /// </summary>
        /// <returns>Deep copy of the device configuration.</returns>
        public override IDeviceConfiguration Clone()
        {
            return new SoundBlasterConfiguration()
            {
                IsEnabled = this.IsEnabled,
                IRQ = this.IRQ,
                DMA8 = this.DMA8,
                DMA16 = this.DMA16
            };
        }
    }
}
